package org.hs.im.forensicj;

import java.awt.Component;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;

import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import javax.swing.JFileChooser;

import org.hs.im.forensicj.view.ImageViewer;

public class DefaultApplication implements Application {
	
	private JFileChooser fileChooser = new JFileChooser();

	private ApplicationWindow window;

	public DefaultApplication( ApplicationWindow window ){
		this.window = window;
		window.setApplication(this);
	}
	
	@Override
	public void openImage() {
		fileChooser.setMultiSelectionEnabled(true);
		fileChooser.setDialogTitle("Choose Image File");
		
		int result = fileChooser.showOpenDialog( getApplicationWindow() );
		
		if( result == JFileChooser.CANCEL_OPTION ) return;
		
		File[] selectedFiles = fileChooser.getSelectedFiles();
		
		for (File file : selectedFiles) {
			openImageFile(file);
		}
		
	}


	@Override
	public void openImageFile(File file) {
		ParameterBlock parameterBlock=new ParameterBlock();
		parameterBlock.add( file.getAbsolutePath() );
		RenderedOp image = JAI.create("fileload",parameterBlock);

		openImage( file.getName(), image);
		
	}


	@Override
	public void openImage(String name, RenderedImage image) {
		window.openImage(name, image);
	}

	private Component getApplicationWindow() {
		return window.getComponent();
	}

	@Override
	public void show() {
		window.setVisible(true);
	}

	@Override
	public ImageViewer getActiveImageViewer() {
		return window.getActiveImageViewer();
	}

}
